﻿using System;
using System.Text;

namespace WholeSlideImageLibrary
{
    internal static class EncodingHelper
    {

        public static void EncodeUtf8(string value, Span<byte> destination)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

#if NO_FAST_SPAN
            unsafe
            {
                fixed (char* pValue = value)
                fixed (byte* pDestination = destination)
                {
                    Encoding.UTF8.GetBytes(pValue, value.Length, pDestination, destination.Length);
                }
            }
#else
            Encoding.UTF8.GetBytes(value, destination);
#endif
        }

        public static string DecodeUtf8(ReadOnlySpan<byte> buffer)
        {
            if (buffer.IsEmpty)
            {
                return string.Empty;
            }
#if NO_FAST_SPAN
            unsafe
            {
                fixed (byte* pData = buffer)
                {
                    return Encoding.UTF8.GetString(pData, buffer.Length);
                }
            }
#else
            return Encoding.UTF8.GetString(buffer);

#endif
        }
    }
}
